package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存信息到dish表
        save(dishDto);
        //菜品口味插入dishId
        dishDto.getFlavors().stream().map(item->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存口味数据到口味表dishflavor
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    /**
     * 查询菜品和口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto queryWithFlavor(Long id) {
        //查询菜品数信息
        Dish dish = getById(id);
        DishDto dishDto = new DishDto();
        //复制菜品信息
        BeanUtils.copyProperties(dish,dishDto);
        //查询口味信息
        List<DishFlavor> flavors = dishFlavorService.list(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, id));
        //设置口味信息
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 修改菜品&口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void editWithFlavor(DishDto dishDto) {
        //更新菜品表
        this.updateById(dishDto);
        //清除当前菜品对应所有口味信息
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId,dishDto.getId()));
        //为口味信息插入dishID字段
        dishDto.getFlavors().stream().map(item->{
            //由于我使用了逻辑删除，主键没有被真正删除，确保新插入数据不是修改前主键
            item.setId(null);
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //插入修改后的口味信息
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteWithFlavor(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        int count = this.count(new LambdaQueryWrapper<Dish>().in(Dish::getId, list).eq(Dish::getStatus, 1));
        if (count > 0) {
            throw new CustomException("当前所选中的商品正在售卖中，不能删除");
        }
        //删除菜品
        this.removeByIds(list);
        //删除菜品关联口味
        LambdaQueryWrapper<DishFlavor> flavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        flavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(flavorLambdaQueryWrapper);
    }
}
